package org.mi.rabbitMQ.subscribe.sender;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Random;

/**
 * 定时器
 */
@Component
@Slf4j
public class Cron {
    @Resource
    private AmqpTemplate amqpTemplate;

    /**
     * 定时发消息
     * 每五秒发送一次
     */
    @Scheduled(cron = "0/5 * * * * ？*")
    public void sender(){
        Random r = new Random();
        Long num = r.nextLong();
        log.debug("Sender : " + num);
        System.out.println("Sender : " + num);
        //往交换机发送消息
        amqpTemplate.convertAndSend("testSubscribe","",num);
    }
}
